{% extends '../_manage.html' %}

{% block title %} {{ _('All Attachments') }} {% endblock %}

{% block head %}
<script>
$(function() {
    getJSON('/api/attachments', {
        page: {{ pageIndex }}
    }, function (err, data) {
        if (err) {
            return fatal(err);
        }
        initVM(data);
    });
});

function initVM(data) {
    var vm = new Vue({
        el: '#vm',
        data: {
            attachments: data.attachments,
            page: data.page
        },
        methods: {
            find: function(id) {
                for (var i = 0; i < this.attachments.length; i ++) {
                    var a = this.attachments[i];
                    if (a.id===id) {
                        return a;
                    }
                }
                throw 'logic error';
            },
            deleteAttachment: function(id) {
                var a = this.find(id);
                if (confirm('Attachment \"' + a.name + '\" will be deleted. Continue?')) {
                    postJSON('/api/attachments/' + a.id + '/delete', function(err, result) {
                        if (err) {
                            return error(err);
                        }
                        refresh();
                    });
                }
            }
        }
    });
    $('#loading').hide();
    $('#vm').show();
}
</script>
{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> Loading...
    </div>

    <div id="error" class="uk-width-1-1">
    </div>

    <div id="vm" class="uk-width-1-1">

        <h3>All Attachments</h3>

        <table class="uk-table uk-table-hover">
            <thead>
                <tr>
                    <th width="20%">Preview</th>
                    <th width="15%">Mime / Size</th>
                    <th width="40%">Name / Description</th>
                    <th width="15%">Created At</th>
                    <th width="10%">&nbsp;</th>
                </tr>
            </thead>
        	<tbody>
                <tr v-repeat="a: attachments">
                    <td>
                        <a v-attr="href: '/files/attachments/' + a.id + '/'" target="_blank"><img v-attr="src: '/files/attachments/' + a.id + '/s'" style="width: 160px;" /></a>
                    </td>
                    <td>
                        <p v-text="a.mime"></p>
                        <p v-text="a.size.toFileSize()"></p>
                        <p v-if="a.width * a.height > 0" v-text="a.width + ' x ' + a.height"></p>
                    </td>
                    <td>
                        <p v-text="a.name"></p>
                        <p v-text="a.description"></p>
                    </td>
                    <td><span v-text="a.created_at.toDateTime()"></span></td>
                    <td>
                        <a v-on="click: deleteAttachment(a.id)" href="#0" title="Delete Attachment" class="x-btn"><i class="uk-icon-trash"></i></a>
                    </td>
                </tr>
                <tr v-if="page.totalItems===0">
                    <td colspan="5">No attachment found.</td>
                </tr>
            </tbody>
        </table>

        <div v-component="pagination" v-with="page"></div>
    </div>

{% endblock %}
